from selenium.webdriver.common.by import By

from common.Utils import ForumDriver

class RegesterTest():
    url=""
    driver=""

    def __init__(self):
        self.url="http://127.0.0.1:9580/sign-up.html"
        self.driver=ForumDriver.driver
        self.driver.get(self.url)

    #成功注册的测试用例
    def RegisterSucTest(self):
        self.driver.find_element(By.CSS_SELECTOR,"#username").clear()
        self.driver.find_element(By.CSS_SELECTOR,"#nickname").clear()
        self.driver.find_element(By.CSS_SELECTOR,"#password").clear()
        self.driver.find_element(By.CSS_SELECTOR,"#passwordRepeat").clear()

        self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys("cat")
        self.driver.find_element(By.CSS_SELECTOR,"#nickname").send_keys("小猫")
        self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys("1234")
        self.driver.find_element(By.CSS_SELECTOR,"#passwordRepeat").send_keys("1234")

        self.driver.find_element(By.CSS_SELECTOR,"#policy").click()
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()

        #是否跳转到登录页面，且用户名密码都自动填好了
        excepted_url="http://127.0.0.1:9580/sign-in.html"
        current_url=self.driver.current_url
        if current_url==excepted_url:
            print("注册成功！可以直接跳转到登录页！")

            excepted_username="xiaoming"
            register_username=self.driver.find_element(By.CSS_SELECTOR,"#username").text
            if register_username==excepted_username:
                print("用户名可以自动填充！")
            else:
                print("用户名不能自动填充！")

            excepted_password="1234"
            register_password=self.driver.find_element(By.CSS_SELECTOR, "#password").text
            if register_password==excepted_password:
                print("密码可以自动填充！")
            else:
                print("密码不能自动填充！")
        else:
            print("未能跳转到登录页！")
        #返回注册页，并截图
        #self.driver.back()
        ForumDriver.getScreenShot()

    def RegisterFailTest(self):
        #1.为空情况，是否在输入框下方有提示文字，打印该提示
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#nickname").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#passwordRepeat").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#policy").click()
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()

        print(self.driver.find_element(By.CSS_SELECTOR,"#signUpForm > div > div:nth-child(2) > div").text)
        print(self.driver.find_element(By.CSS_SELECTOR,"#signUpForm > div > div:nth-child(3) > div").text)
        print(self.driver.find_element(By.CSS_SELECTOR,"#signUpForm > div > div:nth-child(4) > div > div").text)
        print(self.driver.find_element(By.CSS_SELECTOR,"#signUpForm > div > div:nth-child(5) > div > div").text)

        #2.输入已经注册用户名
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("zhangsan")
        self.driver.find_element(By.CSS_SELECTOR, "#nickname").send_keys("111")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("1234")
        self.driver.find_element(By.CSS_SELECTOR, "#passwordRepeat").send_keys("1234")
        self.driver.find_element(By.CSS_SELECTOR, "#policy").click()
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        #会出现警告：用户已存在
        self.driver.find_element(By.CSS_SELECTOR,"body > div.jq-toast-wrap.bottom-right > div > h2")
        ForumDriver.getScreenShot()



